package com.int_tech.timeTask194;

import com.int_tech.MessageCodeAll.dao.CodeA2Dao;
import com.int_tech.MessageCodeAll.dao.CodeA2DoubleDao;
import com.int_tech.MessageCodeAll.entity.CodeA2Double;
import com.int_tech.car.dao.CarDao;
import com.int_tech.car.dao.CarInfoDao;
import com.int_tech.car.dao.CarOpenInfoDao;
import com.int_tech.car.entity.Car;
import com.int_tech.car.entity.CarInInfo;
import com.int_tech.car.entity.CarInfoData;
import com.int_tech.car.entity.CarOpenInfo;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.DateTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/7/22.
 */
@Component
public class AnsyCarOpenInfoTimeTask {

    @Autowired
    private CodeA2DoubleDao codeA2DoubleDao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private StationDao stationDao;

    @Autowired
    private CarInfoDao carInfoDao;

    @Autowired
    private CarOpenInfoDao carOpenInfoDao;

    @Scheduled(cron = "0/10 * * * * ?")
    public void ansyCarOpenInfo() {


        List<CodeA2Double> codeA2DoubleList = codeA2DoubleDao.findByCheckStatus(0);
//        List<CodeA2Double> codeA2DoubleList = codeA2DoubleDao.findByCheckStatusAndCarNumber(0,"010AEA056");

        for (CodeA2Double codeA2Double : codeA2DoubleList) {

            codeA2Double.setCheckStatus(1);
            codeA2DoubleDao.save(codeA2Double);

            try
            {
                if(StringUtils.isEmpty(codeA2Double.getLocation()))
                {
                    //位置信息为空
                    //重新获取一次
                    try
                    {
                        //查询车辆相关联的管理员所属的场站信息
                        Car car = carDao.findCarByCarNumberAndIsDel(codeA2Double.getCarNumber(),0);

                        //车辆所属客户
                        int clientId = car.getClientId();
                        List<Station> stationList = stationDao.findByClientId(clientId);


                        //经纬度
                        String slng = codeA2Double.getLng();
                        String slat = codeA2Double.getLat();

                        Boolean isInStaion = false;

                        //所在场站
                        Station stationStartIn = new Station();

                        for (Station station:stationList)
                        {
                            try
                            {
                                String partitionLocation = "";

                                //场站坐标数组
                                String[] points = station.getArea().split("&");

                                int m1 =0;
                                for (String point:points)
                                {
                                    try
                                    {
                                        if(m1 == 0)
                                        {
                                            partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                                        }
                                        else
                                        {
                                            partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                                        }

                                        m1++;
                                    }
                                    catch (Exception e)
                                    {
                                        e.printStackTrace();
                                    }
                                }

                                if(partitionLocation.length() > 30)
                                {
                                    //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                                    isInStaion = OrderMapTest.isInPolygon(slat,slng,partitionLocation);
                                }
                                else
                                {
                                    //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
//                            System.out.print("要处理的位置信息为"+partitionLocation);
                                    isInStaion = OrderMapTest.isInPolygonSingle(slat,slng,partitionLocation);
                                }

                                if(isInStaion)
                                {
                                    stationStartIn = station;
                                    codeA2Double.setIsInStation("1");
                                    break;
                                }
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                                codeA2Double.setStatus(2);
                            }

                        }
                        if(isInStaion)
                        {
//                            codeA2Double.setLocation(stationStartIn.getName());
                            codeA2Double.setIsInStation("1");

                            //如果在场站内
                            //录入入港信息
                            CarInInfo carInInfo = new CarInInfo();
                            carInInfo.setCarNumber(codeA2Double.getCarNumber());
                            carInInfo.setInTime(DateTool.Date2String(codeA2Double.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
                            //场站id需要查询
                            carInInfo.setStationId(stationStartIn.getId());
                            carInInfo.setStationName(stationStartIn.getName());
                            carInInfo.setCreateTime(codeA2Double.getCreatedTime());
                            carInInfo.setStatus(codeA2Double.getOpenStatus()+1);

                            carInfoDao.save(carInInfo);
                        }
                        else
                        {
//                            codeA2Double.setLocation("场站外"+"("+slat+","+slng+")");
                            codeA2Double.setIsInStation("-1");


                            //异常开门
                            CarOpenInfo carOpenInfo = new CarOpenInfo();
                            carOpenInfo.setCarNumber(codeA2Double.getCarNumber());
                            carOpenInfo.setOpenStatus(codeA2Double.getOpenStatus()+1);

                            //存储起点位置名称
                            String add = AddressParse.getAdd(codeA2Double.getLat(),codeA2Double.getLng());
                            JSONObject jsonObject = JSONObject.fromObject(add);
                            JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
                            JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
                            String endAddress = j_2.getString("name");
                            carOpenInfo.setAddress(endAddress);
                            carOpenInfo.setTime(DateTool.Date2String(codeA2Double.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
                            carOpenInfo.setCreateTime(codeA2Double.getCreatedTime());
                            carOpenInfo.setStationId(car.getStationId());
                            carOpenInfo.setStationName(car.getStationName());
                            carOpenInfo.setLat(codeA2Double.getLat());
                            carOpenInfo.setLng(codeA2Double.getLng());

                            carOpenInfoDao.save(carOpenInfo);
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        codeA2Double.setStatus(2);
                    }

                    codeA2DoubleDao.save(codeA2Double);
                }
                else
                {
                    Boolean isInStaion = false;

                    if("1".equals(codeA2Double.getIsInStation()))
                    {
                        isInStaion = true;
                    }
                    Station stationStartIn = stationDao.findByName(codeA2Double.getLocation());
                    if(isInStaion)
                    {
//                        codeA2Double.setLocation(codeA2Double.getLocation());
                        codeA2Double.setIsInStation("1");

                        //如果在场站内
                        //录入入港信息
                        CarInInfo carInInfo = new CarInInfo();
                        carInInfo.setCarNumber(codeA2Double.getCarNumber());
                        carInInfo.setInTime(DateTool.Date2String(codeA2Double.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
                        //场站id需要查询
                        carInInfo.setStationId(stationStartIn.getId());
                        carInInfo.setStationName(stationStartIn.getName());
                        carInInfo.setCreateTime(codeA2Double.getCreatedTime());
                        carInInfo.setStatus(codeA2Double.getOpenStatus()+1);

                        carInfoDao.save(carInInfo);
                    }
                    else
                    {
//                        codeA2Double.setLocation("场站外"+"("+slat+","+slng+")");
//                        codeA2Double.setIsInStation("-1");


                        //异常开门
                        CarOpenInfo carOpenInfo = new CarOpenInfo();
                        carOpenInfo.setCarNumber(codeA2Double.getCarNumber());
                        carOpenInfo.setOpenStatus(codeA2Double.getOpenStatus()+1);

                        //存储起点位置名称
                        String add = AddressParse.getAdd(codeA2Double.getLat(),codeA2Double.getLng());
                        JSONObject jsonObject = JSONObject.fromObject(add);
                        JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
                        JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
                        String endAddress = j_2.getString("name");
                        carOpenInfo.setAddress(endAddress);
                        carOpenInfo.setTime(DateTool.Date2String(codeA2Double.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
                        carOpenInfo.setCreateTime(codeA2Double.getCreatedTime());
                        if(stationStartIn == null)
                        {
                            Car car = carDao.findCarByCarNumberAndIsDel(codeA2Double.getCarNumber(),0);
                            Station stationNull = stationDao.findById(car.getStationId());
                            carOpenInfo.setStationId(stationNull.getId());
                            carOpenInfo.setStationName(codeA2Double.getLocation());
                        }
                        else
                        {
                            carOpenInfo.setStationId(stationStartIn.getId());
                            carOpenInfo.setStationName(stationStartIn.getName());
                        }
                        carOpenInfo.setLat(codeA2Double.getLat());
                        carOpenInfo.setLng(codeA2Double.getLng());

                        carOpenInfoDao.save(carOpenInfo);
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                codeA2Double.setStatus(-1);
                codeA2DoubleDao.save(codeA2Double);
            }
        }
    }




//    @Scheduled(cron = "0/10 * * * * ?")
    public void carOpenInfoStationChange() {


        List<CarOpenInfo> carOpenInfoList = carOpenInfoDao.findByANameIsNull();

        List<Car> carList = carDao.findCarByIsDel(0);


        for(CarOpenInfo carOpenInfo : carOpenInfoList)
        {
            int m = 0;

            for(Car car : carList)
            {
                if(car.getStationId() == carOpenInfo.getStationId())
                {
                    m = 1;
                    carOpenInfo.setStationId(car.getStationId());
                    carOpenInfo.setAName("1");

                    carOpenInfoDao.save(carOpenInfo);

                    break;
                }
            }


            if(m == 1)
            {
                continue;
            }
        }
    }
}
